package com.example.droidwarehouse.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;

/**
 * Makes storing configurations for an application easy by reading from a
 * properties file
 * 
 * @author Bibek Shrestha <bibekshrestha@gmail.com>
 * @see http 
 *      ://myossdevblog.blogspot.de/2010/02/reading-properties-files-on-android
 *      .html
 */
public class Config
{
    private static String configFile = "config.properties";
    private static Resources resources = null;
    private static AssetManager assetManager = null;
    private static Properties properties = null;
    private static String TAG = "Config";

    public static void setContext(Context context)
    {
        resources = context.getResources();
        assetManager = context.getAssets();

        try
        {
            InputStream inputStream = assetManager.open(configFile);
            properties = new Properties();
            properties.load(inputStream);
            Log.d(TAG, "Configuration loaded");
        }
        catch (IOException e)
        {
            Log.e(TAG, "Failed to open config property file");
            e.printStackTrace();
        }
    }

    /**
     * For given configKey, returns the value specified. Returns null if
     * configKey is not found.
     * 
     * @param configKey
     * @return
     */
    public static Object get(Object configKey)
    {
        if (null == resources || null == assetManager || properties == null)
        {
            throw new RuntimeException("Config class not initialized, please pass context first");
        }

        return properties.get(configKey);
    }
}
